package com.suyang.syt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.suyang.syt.dao.interfaces.CitysDao;
import com.suyang.syt.model.Citys;
import com.suyang.syt.util.EmptyUtil;

@Repository
public class CitysDaoImpl extends BaseDaoImpl<Citys> implements CitysDao {

	private List<Long> datas;

	@Override
	public boolean theExistenceName(String text) {
		String hql = " from Citys c where c.text=? ";
		List<Citys> list = this.list(hql, text);
		if (EmptyUtil.isNotEmpty(list)) {
			return true;
		}
		return false;
	}

	@Override
	public boolean theExistenceNumber(String number) {
		String hql = " from Citys c where c.code=? ";
		List<Citys> list = this.list(hql, number);
		if (EmptyUtil.isNotEmpty(list)) {
			return true;
		}
		return false;
	}

	@Override
	public List<Citys> loadByText(String text) {
		String hql = " from Citys c where c.text=? ";
		return this.list(hql, text);
	}

	@Override
	public void listChidren(long cid) {
		List<Object> list = this.listByObj("select c.id from Citys c where c.pid=?", cid);
		long id = 0;
		for (Object object : list) {
			id = (Long) object;
			datas.add(id);
			listChidren(id);
		}
	}

	@Override
	public String findCitysIds(long cityid) {
		String ids = cityid + ",";
		datas = new ArrayList<Long>();
		listChidren(cityid);
		if (EmptyUtil.isNotEmpty(datas)) {
			for (Long id : datas) {
				ids += id + ",";
			}
		}
		ids = ids.substring(0, ids.length() - 1);
		return ids;
	}

	@Override
	public void updateStatus(String ids, int status) {
		String hql = "update Citys set status=? where id in(" + ids + ")";
		this.executeByHql(hql, status);
	}

}